<template>
	<view class="page">
		<!-- 优惠券兑换 -->
		<!-- <view class="banner-title" v-if="ExchangeType == 0">
			<view class="banner">
				<image src="/static/coupon_default.png" mode=""></image>
			</view>
			<view class="title">
				<view class="name">10元直减优惠券</view>
				<view class="describe">领券当日开始90天内有效</view>
			</view>
			<view class="integral-title">
				<view class="integral">
					<text class="action">1000</text>
					<text>积分</text>
				</view>
				<view class="integral">
					<text>剩余</text>
					<text class="action">1000</text>
					<text>件</text>
				</view>
			</view>
		</view> -->
		<!-- 商品兑换 -->
		<view class="banner-goods" v-for="(value, key) in exchangeGoodsDetails" :key="key">
			<view class="banner">
				<swiper class="screen-swiper round-dot" indicator-dots="true" circular="true" autoplay="true" interval="5000" duration="500">
					<swiper-item v-for="(item, index) in swiperList" :key="index">
						<image :src="item.url" mode="widthFix"></image>
					</swiper-item>
				</swiper>
			</view>
			<view class="goods-title">
				<view class="title-vip">
					<view class="title">
						<text>{{value.getGoodsInfo.goods_name}}</text>
					</view>
					<view class="vip">
						<text>限时活动</text>
					</view>
				</view>
				<view class="integral-title">
					<view class="integral">
						<text class="action">{{value.need_integral}}</text>
						<text>积分</text>
					</view>
					<view class="integral">
						<text>剩余</text>
						<text class="action">{{value.stock}}</text>
						<text>件</text>
					</view>
				</view>
			</view>
		</view>
		<!-- 兑换须知 -->
		<view class="convertible-notes">
			<view class="title">兑换须知</view>
			<view class="content">
				<view>1、兑换商品;</view>
				<view>2、骄傲无合法u王府啊我佛i啊好我i发哈我;</view>
			</view>
		</view>
		<!-- 兑换按钮 -->
		<view class="exchange-btn">
			<view class="btn" @click="isExchange = true">立即兑换</view>
		</view>
		<!-- 兑换弹窗 -->
		<view class="exchange-win">
			<view class="cu-modal" :class="{'show':isExchange}">
			  <view class="cu-dialog">
			    <view class="bg-img" style="background-image: url('/static/img/banner_04.png');height:200px;">
			      <view class="cu-bar justify-end text-white">
			        <view class="action">
			          <text class="cuIcon-close" @click="isExchange = false"></text>
			        </view>
			      </view>
			    </view>
			    <view class="goods-title" v-for="(value, key) in exchangeGoodsDetails" :key="key">
			    	<view class="title-vip">
			    		<view class="title">
			    			<text class="two-omit">{{value.getGoodsInfo.goods_name}}</text>
			    		</view>
			    		<view class="vip">
			    			<text>限时活动</text>
			    		</view>
			    	</view>
			    	<view class="integral-title">
			    		<view class="integral">
			    			<text class="action">{{value.need_integral}}</text>
			    			<text>积分</text>
			    		</view>
			    		<view class="integral">
			    			<text>剩余</text>
			    			<text class="action">{{value.stock}}</text>
			    			<text>件</text>
			    		</view>
			    	</view>
			    </view>
					<view class="delivery-address" @click="Address">
						<view class="title">收货地址</view>
						<view class="address-phone">
							<view class="icon">
								<text class="iconfont icon-dingwei1"></text>
							</view>
							<view class="address">
								<view class="location">
									<text class="one-omit">{{myAddress.address}}</text>
								</view>
								<view class="name-phone">
									<text>{{myAddress.name}}</text>
									<text>{{myAddress.phone}}</text>
								</view>
							</view>
							<view class="more">
								<text class="iconfont icon-more"></text>
							</view>
						</view>
					</view>
					<view class="affirm-btn">
						<view class="btn" @click="onAffirm">确认</view>
					</view>
			  </view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				ExchangeType: 0,
				isExchange: false,
				exchangeGoodsDetails:[],
				myAddress:[],
        swiperList: [
          {
            id: 0,
            type: 'image',
            url: '/static/img/banner_01.png'
          },
          {
            id: 1,
            type: 'image',
            url: '/static/img/banner_02.png'
          },
          {
            id: 2,
            type: 'image',
            url: '/static/img/banner_03.png'
          }
        ],
			};
		},
		onLoad(params) {
			console.log(params)
			this.ExchangeType = params.type||0;
			this.$http.get('exchangeGoodsDetails', {
				id:params.id
			}).then(res => {
				
				this.exchangeGoodsDetails = res.data.data
			})
			

			this.$http.get('user/default/address', {
			}).then(res => {
				console.log(res)
				this.myAddress = res.data
			})
			
			
			
		},
		onShow() {
			let pages = getCurrentPages(); 
			let currPage = pages[pages.length-1]; 
			console.log(currPage.$vm.aid)
			if(currPage.$vm.aid){
				this.$http.get('user/addressByDetail', {
					eid:currPage.$vm.aid
				}).then(res => {
					this.myAddress = res.data.data
				})
			}else{
				return;
			}
			
		},
		methods:{
			/**
			 * 兑换确认点击
			 */
			onAffirm(){
				var integral = uni.getStorageSync("userInfo").integral;
				var needintegral = this.exchangeGoodsDetails[0].need_integral
				if(integral < needintegral){
					this.isExchange = false
					uni.showToast({
						title: "积分不足",
						duration: 2000,
						icon:"none"
					})
					return;
				}
				this.$http.post('orderIntegralGoods', {
					id:this.exchangeGoodsDetails[0].id,
					aid:this.myAddress.id
				}).then(res => {
					if(res.data.code == 200){
						uni.navigateTo({
							url: '/pages/ExchangeResult/ExchangeResult?type=' + this.ExchangeType + "&aid=" + this.myAddress.id,
						})
					}
				})
				
				
			},
			Address(){
				uni.navigateTo({
					url:"/pages/Address/Address",
					events:{
						acceptDataFromOpenedPage(res) {
							console.log(res)
						}
					}
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	@import 'ExchangeDetails.scss';
</style>
